﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using System.IO;
namespace QUANLYDIEMHOCSINH
{
    public partial class GVNHAPDIEM : Form
    {
        DIEMBUS diembus = new DIEMBUS();
        MONHOCBUS monbus = new MONHOCBUS();
        HOCKYBUS hkbus = new HOCKYBUS();
        LOPBUS lopbus = new LOPBUS();
        NAMHOCBUS nambus = new NAMHOCBUS();
        public GVNHAPDIEM()
        {
            InitializeComponent();
            set_AN();
            LoadNamHoc();
            LoadDanhSachMon();
            LoadDanhSachHocKy();
            LoadDanhSachLop();
            LoadDanhSachDiem(int.Parse(cbLop.SelectedValue.ToString()), int.Parse(cbHocKy.SelectedValue.ToString()), int.Parse(cbMon.SelectedValue.ToString()));
        }
        void set_AN()
        {
            if (File.Exists("AnhNen.txt"))
            {
                FileStream fs = new FileStream("AnhNen.txt", FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string s = null;
                string t = null;
                while ((s = sr.ReadLine()) != null)
                {
                    t = s;
                }
                this.BackColor = Color.FromName(t);
                fs.Close();
            }
            else
            {
                FileStream fs = new FileStream("AnhNen.txt", FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine("Turquoise");
                sw.Flush();
                sw.Close();
                fs.Close();

                FileStream fs1 = new FileStream("AnhNen.txt", FileMode.Open);
                StreamReader sr1 = new StreamReader(fs1);
                string s = null;
                string t = null;
                while ((s = sr1.ReadLine()) != null)
                {
                    t = s;
                }
                this.BackColor = Color.FromName(t);
                fs1.Close();
            }
        }
        void LoadDanhSachDiem(int malop, int mahocky, int mamon)
        {
            DataTable dt = diembus.LoadDiem(malop, mahocky, mamon);
            dtgrwDiem.DataSource = dt;
            for (int i = 0; i < dtgrwDiem.Rows.Count; i++)
            {
                dtgrwDiem.Rows[i].Cells["c1"].Value = (i + 1).ToString();
            }
            dtgrwDiem.Refresh();
        }

        void LoadDanhSachMon()
        {
            cbMon.DataSource = monbus.LoadDanhSachMon();
            cbMon.DisplayMember = "TENMH";
            cbMon.ValueMember = "MAMH";
            cbMon.SelectedIndex = 0;
        }

        void LoadDanhSachHocKy()
        {
            cbHocKy.DataSource = hkbus.LoadDachHocKy();
            cbHocKy.DisplayMember = "TENHK";
            cbHocKy.ValueMember = "MAHK";
            cbHocKy.SelectedIndex = 0;
        }

        void LoadDanhSachLop()
        {
            cbLop.DataSource = lopbus.layDSL();
            cbLop.DisplayMember = "TENLOP";
            cbLop.ValueMember = "MALOP";
            cbLop.SelectedItem = 0;
        }

        void LoadNamHoc()
        {
            NAMHOCDTO namhoc = nambus.LoadNamHocHienTai();
            txtNamHoc.Text = namhoc.NAMBATDAU + " - " + namhoc.NAMKETTHUC;
        }

        private void dtgrwDiem_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex <= 16 && e.ColumnIndex >=3)
            {
                DIEMDTO d = new DIEMDTO();
                d.MAHS = int.Parse(dtgrwDiem.Rows[e.RowIndex].Cells["c0"].Value.ToString());
                d.MAHK = int.Parse(cbHocKy.SelectedValue.ToString());
                d.MAMH = int.Parse(cbMon.SelectedValue.ToString());
                if (dtgrwDiem.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "")
                {
                    if (dtgrwDiem.Rows[e.RowIndex].Cells[e.ColumnIndex + 14].Value.ToString() != "")
                    {
                        diembus.XoaDiem(int.Parse(dtgrwDiem.Rows[e.RowIndex].Cells[e.ColumnIndex + 14].Value.ToString()));
                        dtgrwDiem.Rows[e.RowIndex].Cells[e.ColumnIndex + 14].Value = "";
                        return;
                    }
                }
                else
                {
                    d.DIEM = float.Parse(dtgrwDiem.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
                }
                if (e.ColumnIndex == 3 || e.ColumnIndex == 4 || e.ColumnIndex == 5)
                {
                    d.MALCD = 1;
                }
                if (e.ColumnIndex == 6 || e.ColumnIndex == 7 || e.ColumnIndex == 8 || e.ColumnIndex == 9 || e.ColumnIndex == 10)
                {
                    d.MALCD = 2;
                }
                if (e.ColumnIndex == 11 || e.ColumnIndex == 12 || e.ColumnIndex == 13 || e.ColumnIndex == 14 || e.ColumnIndex == 15)
                {
                    d.MALCD = 3;
                }
                if (e.ColumnIndex == 16)
                {
                    d.MALCD = 4;
                }
                if (dtgrwDiem.Rows[e.RowIndex].Cells[e.ColumnIndex + 14].Value.ToString() == "")
                {
                    dtgrwDiem.Rows[e.RowIndex].Cells[e.ColumnIndex + 14].Value = diembus.ThemDiem(d);
                    return;
                }
                else
                { 
                    diembus.XoaDiem(int.Parse(dtgrwDiem.Rows[e.RowIndex].Cells[e.ColumnIndex + 14].Value.ToString()));
                    dtgrwDiem.Rows[e.RowIndex].Cells[e.ColumnIndex + 14].Value = diembus.ThemDiem(d);
                    return;
                }
            }
            else
            {
                return;
            }
        }

        private void cbLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDanhSachDiem(int.Parse(cbLop.SelectedValue.ToString()), int.Parse(cbHocKy.SelectedValue.ToString()), int.Parse(cbMon.SelectedValue.ToString()));
        }

        private void cbMon_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDanhSachDiem(int.Parse(cbLop.SelectedValue.ToString()), int.Parse(cbHocKy.SelectedValue.ToString()), int.Parse(cbMon.SelectedValue.ToString()));
        }

        private void cbHocKy_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDanhSachDiem(int.Parse(cbLop.SelectedValue.ToString()), int.Parse(cbHocKy.SelectedValue.ToString()), int.Parse(cbMon.SelectedValue.ToString()));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            dtgrwDiem.DataSource = diembus.LoadDiemTheoTen(int.Parse(cbHocKy.SelectedValue.ToString()), int.Parse(cbMon.SelectedValue.ToString()),txtTimKiem.Text);
        }

        private void dtgrwDiem_Enter(object sender, EventArgs e)
        {
            
        }

        private void dtgrwDiem_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (this.dtgrwDiem.CurrentCellAddress.X >= 3 && this.dtgrwDiem.CurrentCellAddress.X <= 16)
            {
                TextBox txt = e.Control as TextBox;
                txt.Name = "key";
                if (txt != null)
                {
                    txt.KeyPress -= new KeyPressEventHandler(txt_keypress);
                    txt.KeyPress += new KeyPressEventHandler(txt_keypress);
                }

            }
        }
        private void txt_keypress(object sender, KeyPressEventArgs e)
        {
            TextBox txt = sender as TextBox;
            if(txt.Name=="key")
            {
                if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar!='.')
                {
                    e.Handled = true;
                }
            }

        }

        private void btnInBangDiem_Click(object sender, EventArgs e)
        {
            frmDiemNhap ds = new frmDiemNhap();
            ds.MaMon = int.Parse(cbMon.SelectedValue.ToString());
            ds.MaHocKy = int.Parse(cbHocKy.SelectedValue.ToString());
            ds.MaLop = int.Parse(cbLop.SelectedValue.ToString());
            ds.NamHoc = txtNamHoc.Text;
            ds.HocKy = cbHocKy.Text;
            ds.Lop = cbLop.Text;
            ds.Mon = cbMon.Text;
            ds.ShowDialog();
        }

        private void dtgrwDiem_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            dtgrwDiem.Rows[e.RowIndex].Cells["c1"].Value = e.RowIndex + 1;
        }
    }
}
